package com.iOS.controller;

import com.iOS.dto.PushInfoExecution;
import com.iOS.dto.UserInfoExecution;
import com.iOS.entity.PushInfo;
import com.iOS.enums.PushInfoEnum;
import com.iOS.service.IPushInfoService;
import com.iOS.service.IUserInfoService;
import com.iOS.util.HttpServletRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/push")
public class PushInfoController {
    @Autowired
    private IPushInfoService pushInfoService;
    @Autowired
    private IUserInfoService userInfoService;

    @RequestMapping(value = "/addition", method = {RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> Addition(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        String pushTitle= HttpServletRequestUtil.getString(request,"pushTitle");
        String pushContext=HttpServletRequestUtil.getString(request,"pushContext");
        String token = HttpServletRequestUtil.getString(request, "token");
         /*获取图片*/
        MultipartResolver MultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        MultipartHttpServletRequest multipartHttpServletRequest =MultipartResolver.resolveMultipart(request);
        MultipartFile img = multipartHttpServletRequest.getFile("pushPictures");
        if (token==null){
            modelMap.put("success",false);
        }else {
            try {
                UserInfoExecution user = userInfoService.GetUserInfo(token);
                PushInfo pushInfo=new PushInfo();
                int userId=user.getUserInfo().getId();
                pushInfo.setUserId(userId);
                pushInfo.setPushTitle(pushTitle);
                pushInfo.setPushContext(pushContext);
                pushInfo.setPushPictures(img.getOriginalFilename());
                PushInfoExecution addition=pushInfoService.Add(pushInfo);
                modelMap.put("success", addition.getState());
                if (addition.getState()== PushInfoEnum.SUCCESS.getState()){
                    modelMap.put("success", true);
                    modelMap.put("userInfo", addition.getPushInfo());
                }
            } catch (Exception e) {
                modelMap.put("success", false);
                modelMap.put("errMsg", e.getMessage());
            }
        }
        return modelMap;
    }

    @RequestMapping(value = "/query",method = {RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> List(Model model) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
       /*获取列表页*/
        List<PushInfo> list = pushInfoService.getPushList();
        model.addAttribute("list",list);
        return modelMap;
    }



    @RequestMapping(value = "/{ID}/detail",method = {RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> detail(@PathVariable("Id") int id, Model model){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        PushInfo pushInfo = pushInfoService.getById(id);
        model.addAttribute("pushInfo",pushInfo);
        return modelMap;
    }



    @RequestMapping(value = "/{ID}/delete",method = {RequestMethod.GET})
    @ResponseBody
    public  Map<String, Object> delete(@PathVariable("Id") int id, Model model){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        pushInfoService.Delete(id);
        List<PushInfo> list = pushInfoService.getPushList();
        model.addAttribute("list",list);
        return modelMap;
    }

}
